new Vue({
    el: '#app',
    data: function () {
        return {
            submitBtnFlag: false,
            data1: 0,
            data2: 0,
            alertMsg: []
        }
    },
    mounted: function () {
        //this.initWebSocket();
        // 生成随机数，回填默认数
        this.fillDefaultData();
    },
    methods: {
        /**
         * websocket客户端初始化
         */
        initWebSocket: function () {
            var me = this;
            var websocket = new WebSocket("ws://localhost:9998/alarmWebsocket");

            //连接发生错误的回调方法
            websocket.onerror = function () {
                me.$notify.error({title: '', message: 'Socket 连接失败！'});
            };

            //连接成功建立的回调方法
            websocket.onopen = function (event) {
                me.$notify.success({title: '', message: 'Socket 连接成功！'});
            }

            //接收到消息的回调方法
            websocket.onmessage = function (event) {
                //setMessageInnerHTML(event.data);
                console.log("接收到服务端消息：" + event.data);
                me.alertMsg.push(event.data);
            }

            //连接关闭的回调方法
            websocket.onclose = function () {
                me.$notify.error({title: '', message: 'Socket 连接关闭！'});
            }

            //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
            window.onbeforeunload = function () {
                websocket.close();
            }


        },


        //生成从minNum到maxNum的随机数
        randomNum: function (minNum, maxNum) {
            switch (arguments.length) {
                case 1:
                    return parseInt(Math.random() * minNum + 1, 10);
                    break;
                case 2:
                    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                    break;
                default:
                    return 0;
                    break;
            }
        },

        /**
         * 给默认数据
         */
        fillDefaultData: function () {
            var defaultData1 = this.randomNum(700, 2000);
            var defaultData2 = this.randomNum(700, 2000);

            this.data1 = defaultData1;
            this.data2 = defaultData2;
        },

        /**
         * 提交数据
         */
        doSubmit: function () {
            var me = this;
            var data = {
                data1: this.data1,
                data2: this.data1
            };
            //
            me.alertMsg = [];
            me.submitBtnFlag = true;
            $.ajax({
                type: "POST",
                contentType: 'application/json',
                dataType: "json",
                url: '../fillData/submit',
                data: JSON.stringify(data),
                success: function (jsonData) {
                    setTimeout(function () {
                        me.submitBtnFlag = false;
                    }, 1000);

                    if (jsonData.code == 200) {
                        //me.$message.success("数据提交成功！");
                        me.$notify.success({title: '', message: '数据提交成功！'});
                        // 重新赋默认值
                        me.fillDefaultData();
                    } else {
                        // me.$message.error("数据提交失败！");
                        this.$notify.error({title: '', message: '数据提交失败！'});
                    }
                }
            });
        }

    },

});
